package com.hejia.alauda.persistence.mybatis;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hejia.alauda.entity.BonusOrder;
import com.hejia.alauda.entity.BonusRecord;
import com.hejia.alauda.entity.BonuseRepaymentCount;
import com.hejia.alauda.persistence.BaseMapper;

public interface BonusOrderMapper extends BaseMapper<BonusOrder> {

	/**
	 * 更新订单激活的红包为已打款状态
	 * @param orderId 订单ID
	 * @param repayOrderId	还款订单ID
	 * @return
	 */
	@Update("update `bonusorder` a,bonusRecord b set repaid=1,a.modifyDate=now() where a.bonusRecord_id=b.id and repaid=0 and a.inPay=True and a.repayOrder_id = #{repayOrderId} and a.order_id = #{orderId}")
	int updateBonusOrderRepaidStatus(
			@Param("orderId") Long orderId,
			@Param("repayOrderId") Long repayOrderId);

	/**
	 * 设置订单激活未打款的红包记录为支付进行中
	 * @param orderId	订单ID
	 * @return
	 */
	@Update("update `bonusorder` a,bonusRecord b set a.inPay=True,a.modifyDate=now() where a.bonusRecord_id=b.id and repayOrder_id is null and repaid=0 and a.order_id = #{orderId}")
	int setBonusOrderInPay(@Param("orderId") Long orderId);

	/**
	 * 设置订单激活正在打款的红包的还款订单
	 * @param orderId 订单ID
	 * @param repayOrderId	还款订单ID
	 * @return
	 */
	@Update("update `bonusorder` a, bonusRecord b set a.repayOrder_id=#{repayOrderId},a.modifyDate=now() where a.bonusRecord_id=b.id and repayOrder_id is null and repaid=0 and a.inPay=True and a.order_id=#{orderId}")
	public int setBonusRepayOrder(
			@Param("orderId") Long orderId,
			@Param("repayOrderId") Long repayOrderId);

	/**
	 * 根据用户id获取用户未还总红包金额
	 * 
	 * @param userId
	 * @return
	 */
	@Select("select ifnull(sum(a.amount),0) from bonusorder a,bonusRecord b  where a.bonusRecord_id=b.id and b.user_id=#{userId} and repaid=0 and (a.inPay=False or a.inPay is null)")
	public Double sumBonusByUserId(Long userId);
	
	@Select("SELECT * FROM bonusorder WHERE bonusRecord_id=#{id} LIMIT 1")
	@ResultMap(value="DetailMap")
	BonusOrder findBonusOrderByBonusRecord(BonusRecord bonusRecord);

    /**
     * 计算订单激活未打款的红包金额
     * @param orderId 订单ID
     * @return	订单激活未打款的红包金额
     */
	@Select("select ifnull(sum(amount), 0) from bonusorder where repayOrder_id is null and repaid = false and (inPay = false or inPay is null) and order_id = #{orderId}")
    public Double sumBonusNotPayAmountByOrderId(Long orderId);

    /**
     * 查询红包打款申请列表
     * @param startNumber	开始的数量
     * @param pageSize		每页记录数
     * @param description	红包描述
     * @return
     */
    @Select("select o.* from bonusorder o join bonusrecord r on o.bonusRecord_id = r.id join user u on r.user_id = u.id where o.repaid = 0 and (o.inPay = 0 or o.inPay is null) and o.repayOrder_id is null and (r.description like #{description} or u.realName like #{description} or u.mobile like #{description}) limit #{startNumber}, #{pageSize}")
    @ResultMap(value="DetailMap")
    List<BonusOrder> selectApplyBonusOrders(
			@Param("startNumber") Integer startNumber, 
			@Param("pageSize") Integer pageSize, 
			@Param("description") String description);

    /**
     * 统计查询红包打款申请总数
     * @param description	红包描述
     * @return	红包打款申请总数
     */
    @Select("select count(o.id) from bonusorder o join bonusrecord r on o.bonusRecord_id = r.id join user u on r.user_id = u.id where o.repaid = 0 and (o.inPay = 0 or o.inPay is null) and o.repayOrder_id is null and (r.description like #{description} or u.realName like #{description} or u.mobile like #{description})")
	Integer countApplyBonusOrders(@Param("description") String description);

    /**
     * 更新红包订单为已打款状态
     * @param repaymentOrderId	还款订单ID
     * @return
     */
    @Update("update bonusorder set repaid = 1, modifyDate = now() where repayOrder_id = #{repaymentOrderId} and inPay = 1")
	Integer updateBonusOrderRepaidByRepaymentOrderId(@Param("repaymentOrderId") Long repaymentOrderId);

    /**
     * 统计待还款红包数据
     * @return
     */
    @Select("select ifnull(sum(o.amount), 0) waitRepayAmount, ifnull(count(o.id), 0) waitRepayCount from bonusorder o join bonusrecord r on o.bonusRecord_id = r.id where o.repaid = 0 and (o.inPay = 0 or o.inPay is null) and o.repayOrder_id is null")
	BonuseRepaymentCount countWaitRepaymentBonuse();

    @Select("select count(id) count, ifnull(sum(amount), 0) amount from bonusrecord where consume = 0 and date(expiredDate) >= date(now()) and code = #{code} and user_id = #{userId}")
	Map<String, Object> countCanCashBonusesByCode(@Param("userId") Long userId, @Param("code") String code);
}
